package bean;



import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;
import org.hibernate.Session;

import po.Address;
import po.User;

import bo.HibernateSessionFactory;

public class AddressDb {

	public void AddressAddP(String name,String email,String belong){
		Session session = HibernateSessionFactory.getSession();
		HttpServletRequest request = ServletActionContext.getRequest();
		User user = new User((Integer)request.getSession().getAttribute("id"));
		Address a = new Address(user,name,email,belong);
		Set set = new HashSet();
		set.add(a);
		a.setUser(user);
		session.save(a);
		session.beginTransaction().commit();
		session.close();
	}
	
	public List<Address> getAddressList(Integer id,String flag){
		
		List<Address> list = new ArrayList<Address>();
		
		Session session = HibernateSessionFactory.getSession();
		User user = (User)session.get(User.class, id);
		Set set = user.getAddresses();
		Iterator it = set.iterator();
		while(it.hasNext()){
			Address a = (Address)it.next();
			if((flag.equals("f"))&&a.getBelong().equals("朋友")){
				list.add(a);
			}
			if((flag.equals("r"))&&a.getBelong().equals("亲人")){
				list.add(a);
			}
			if((flag.equals("co"))&&a.getBelong().equals("同事")){
				list.add(a);
			}
			if((flag.equals("n"))&&a.getBelong().equals("网友")){
				list.add(a);
			}
			if((flag.equals("cm"))&&a.getBelong().equals("常用联系人")){
				list.add(a);
			}
			if((flag.equals("o"))&&a.getBelong().equals("其他")){
				list.add(a);
			}
		}
		session.close();
		return list;
	}
	
	public int getAddress(String flag,Integer id){
		int num = 0;
		Session session = HibernateSessionFactory.getSession();
		Query query = null;
		if(flag.equals("f")){
			query = session.createQuery("from Address where belong='朋友' and u_id="+id);
		}
		if(flag.equals("r")){
			query = session.createQuery("from Address where belong='亲人' and u_id="+id);
		}
		if(flag.equals("co")){
			query = session.createQuery("from Address where belong='同事' and u_id="+id);
		}
		if(flag.equals("n")){
			query = session.createQuery("from Address where belong='网友' and u_id="+id);
		}
		if(flag.equals("cm")){
			query = session.createQuery("from Address where belong='常用联系人' and u_id="+id);
		}
		if(flag.equals("o")){
			query = session.createQuery("from Address where belong='其他' and u_id="+id);
		}
		return query.list().size();
	}
	
	public List getAddressone(String flag,int page,Integer id){
		Session session = HibernateSessionFactory.getSession();
		Query query = null;
		if(flag.equals("f")){
			query = session.createQuery("from Address where belong='朋友' and u_id="+id);
			query.setFirstResult(page*2);
			query.setMaxResults(2);
		}
		if(flag.equals("r")){
			
			query = session.createQuery("from Address where belong='亲人' and u_id="+id);
			query.setFirstResult(page*2);
			query.setMaxResults(2);
		}
		if(flag.equals("co")){
			query = session.createQuery("from Address where belong='同事' and u_id="+id);
			query.setFirstResult(page*2);
			query.setMaxResults(2);
		}
		if(flag.equals("n")){
			query = session.createQuery("from Address where belong='网友' and u_id="+id);
			query.setFirstResult(page*2);
			query.setMaxResults(2);
		}
		if(flag.equals("cm")){
			query = session.createQuery("from Address where belong='常用联系人' and u_id="+id);
			query.setFirstResult(page*2);
			query.setMaxResults(2);
		}
		if(flag.equals("o")){
			query = session.createQuery("from Address where belong='其他' and u_id="+id);
			query.setFirstResult(page*2);
			query.setMaxResults(2);
		}
		return query.list();
		
	}

}
